/*
*	Copyright (C) 2014  Julio Montes, Mario Carrillo
*	
*	Julio Montes e-mail: imc.coder@gmail.com 
*
*	Mario Carrillo e-mail: mario_90ago@live.com.mx
*
*	BSD 3-Clause License
*/

#include "alliance/include/envarpanel.h"

namespace alliance
{
    EnvarPanel::EnvarPanel( wxWindow *parent, wxWindowID id )
        : wxPanel( parent, wxID_ANY )
        , m_index( 0 )
    {
        wxStaticBoxSizer *l_boxSizer = new wxStaticBoxSizer( wxHORIZONTAL, this, wxT( " Environment variables " ) );
        m_listCtrl = new wxListCtrl( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_SINGLE_SEL | wxLC_REPORT );

        wxListItem l_col0;
        l_col0.SetId( 0 );
        l_col0.SetText( wxT("Name") );
        l_col0.SetWidth( 200 );
        m_listCtrl->InsertColumn( 0, l_col0 );

        wxListItem l_col1;
        l_col1.SetId( 1 );
        l_col1.SetText( wxT("Value") );
        l_col1.SetWidth( 250 );
        m_listCtrl->InsertColumn( 1, l_col1 );
        l_boxSizer->Add( m_listCtrl, 1, wxEXPAND | wxALL, 5 );


        ///Buttons
        wxBoxSizer *l_btnBoxSizer = new wxBoxSizer( wxVERTICAL );
        wxButton *l_btnAdd = new wxButton( this, wxID_ADD );
        Connect( wxID_ADD, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( EnvarPanel::OnAdd ) );
        l_btnBoxSizer->Add( l_btnAdd, 0, wxEXPAND );
        l_btnBoxSizer->AddSpacer( 5 );

        wxButton *l_btnNew = new wxButton( this, wxID_NEW );
        Connect( wxID_NEW, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( EnvarPanel::OnNew ) );
        l_btnBoxSizer->Add( l_btnNew, 0, wxEXPAND );
        l_btnBoxSizer->AddSpacer( 5 );

        wxButton *l_btnRemove = new wxButton( this, wxID_REMOVE );
        Connect( wxID_REMOVE, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( EnvarPanel::OnRemove ) );
        l_btnBoxSizer->Add( l_btnRemove, 0, wxEXPAND );
        l_btnBoxSizer->AddSpacer( 5 );

        wxButton *l_btnEdit = new wxButton( this, wxID_EDIT );
        Connect( wxID_EDIT, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( EnvarPanel::OnEdit ) );
        l_btnBoxSizer->Add( l_btnEdit, 0, wxEXPAND );
        l_btnBoxSizer->AddSpacer( 5 );

        l_boxSizer->Add( l_btnBoxSizer, 0, wxEXPAND | wxALL, 5 );

        SetSizer( l_boxSizer );

    }

    EnvarPanel::~EnvarPanel()
    {

    }

    void EnvarPanel::AddVariable( const wxString &name, const wxString &value )
    {
        wxListItem item;
        item.SetId( m_index );
        m_listCtrl->InsertItem( item );

        m_listCtrl->SetItem( m_index, 0, name );
        m_listCtrl->SetItem( m_index, 1, value );
        ++m_index;
    }

    size_t EnvarPanel::GetVariables( VectorVariables *vectorVariables )
    {
        for( size_t i=0; i<m_index; ++i )
        {
            EnvironmentVariable l_variable;
            wxListItem row_info;
            row_info.m_itemId = i;
            row_info.m_mask = wxLIST_MASK_TEXT;

            row_info.m_col = 0;
            m_listCtrl->GetItem( row_info );
            l_variable.name = row_info.m_text;

            row_info.m_col = 1;
            m_listCtrl->GetItem( row_info );
            l_variable.value = row_info.m_text;

            vectorVariables->push_back( l_variable );
        }

        return m_index;
    }

    void EnvarPanel::OnAdd( wxCommandEvent &evt )
    {
        ChooserVariablesDialog l_dlg( GetParent() );
        l_dlg.Centre();
        if( l_dlg.ShowModal() == wxID_OK )
        {
            const EnvironmentVariable &l_envar = l_dlg.GetVariable();
            AddVariable( l_envar.name, l_envar.value );
        }
    }

    void EnvarPanel::OnRemove( wxCommandEvent &evt )
    {
        long l_item = m_listCtrl->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
        if( l_item != -1 )
        {
            m_listCtrl->DeleteItem( l_item );
            --m_index;
        }
    }

    void EnvarPanel::OnEdit( wxCommandEvent &evt )
    {
        long l_item = m_listCtrl->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
        if( l_item != -1 )
        {
            wxListItem row_info;
            row_info.m_itemId = l_item;
            row_info.m_col = 1;
            row_info.m_mask = wxLIST_MASK_TEXT;
            m_listCtrl->GetItem( row_info );

            NewVariableDialog l_dlg( GetParent(), wxID_ANY, m_listCtrl->GetItemText( l_item ), row_info.m_text );
            l_dlg.Centre();
            if( l_dlg.ShowModal() == wxID_OK )
            {
                const EnvironmentVariable &l_envar = l_dlg.GetVariable();
                m_listCtrl->SetItem( l_item, 0, l_envar.name );
                m_listCtrl->SetItem( l_item, 1, l_envar.value );
            }
        }
    }

    void EnvarPanel::OnNew( wxCommandEvent &evt )
    {
        NewVariableDialog l_dlg( GetParent() );
        l_dlg.Centre();
        if( l_dlg.ShowModal() == wxID_OK )
        {
            const EnvironmentVariable &l_envar = l_dlg.GetVariable();
            AddVariable( l_envar.name, l_envar.value );
        }
    }










    ChooserVariablesDialog::ChooserVariablesDialog( wxWindow *parent )
        : wxDialog( parent, wxID_ANY, wxT( "Choose a environment variable" ) )
        , m_lbEnvironmentVariables( NULL )
        , m_tcVariableValue( NULL )
    {
        wxBoxSizer *bsMain = new wxBoxSizer( wxVERTICAL );



        ///ENVIRONMENT VARIABLE
        wxBoxSizer *bsEnvironment = new wxBoxSizer( wxVERTICAL );

        wxArrayString aEnvVariables;
        aEnvVariables.Add( L"ALLIANCE_TOP" );
        aEnvVariables.Add( L"DREAL_TECHNO_NAME" );
        aEnvVariables.Add( L"ELP_TECHNO_NAME" );
        aEnvVariables.Add( L"GENVIEW_TECHNO_NAME" );
        aEnvVariables.Add( L"GRAAL_TECHNO_NAME" );
        aEnvVariables.Add( L"LD_LIBRARY_PATH" );
        aEnvVariables.Add( L"MBK_C4_LIB" );
        aEnvVariables.Add( L"MBK_CATAL_NAME" );
        aEnvVariables.Add( L"MBK_CATA_LIB" );
        aEnvVariables.Add( L"MBK_IN_LO" );
        aEnvVariables.Add( L"MBK_IN_PH" );
        aEnvVariables.Add( L"MBK_OUT_LO" );
        aEnvVariables.Add( L"MBK_OUT_PH" );
        aEnvVariables.Add( L"MBK_SCALE_X" );
        aEnvVariables.Add( L"MBK_SPI_MODEL" );
        aEnvVariables.Add( L"MBK_TARGET_LIB" );
        aEnvVariables.Add( L"MBK_VDD" );
        aEnvVariables.Add( L"MBK_VSS" );
        aEnvVariables.Add( L"MBK_WORK_LIB" );
        aEnvVariables.Add( L"RDS_IN" );
        aEnvVariables.Add( L"RDS_OUT" );
        aEnvVariables.Add( L"RDS_TECHNO_NAME" );
        aEnvVariables.Add( L"VH_BEHSFX" );
        aEnvVariables.Add( L"VH_DLYSFX" );
        aEnvVariables.Add( L"VH_MAXERR" );
        aEnvVariables.Add( L"VH_PATSFX" );
        aEnvVariables.Add( L"XFSM_PARAM_NAME" );
        aEnvVariables.Add( L"XPAT_PARAM_NAME" );
        aEnvVariables.Add( L"XSCH_PARAM_NAME" );
        aEnvVariables.Sort();
        m_lbEnvironmentVariables = new wxListBox( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, aEnvVariables );
        bsEnvironment->Add( m_lbEnvironmentVariables, 1, wxEXPAND );
        m_lbEnvironmentVariables->SetFocus();

        bsEnvironment->AddSpacer( 10 );
        wxBoxSizer *bsInputValue = new wxBoxSizer( wxHORIZONTAL );
        wxStaticText *stVariableValue = new wxStaticText( this, wxID_ANY, wxT( "Value: " ) );
        m_tcVariableValue = new wxTextCtrl( this, wxID_ANY, wxT( "" ) );
        bsInputValue->Add( stVariableValue, 0, wxALIGN_CENTER_VERTICAL );
        bsInputValue->AddSpacer( 5 );
        bsInputValue->Add( m_tcVariableValue, 1 );
        bsEnvironment->Add( bsInputValue, 0, wxEXPAND );

        bsMain->Add( bsEnvironment, 1, wxEXPAND | wxRIGHT | wxLEFT, 10 );






        ///BOTONES CANCELAR, ACEPTAR
        bsMain->AddSpacer( 20 );
        wxBoxSizer *bsButtonsBottom = new wxBoxSizer( wxHORIZONTAL );
        wxButton *btnCancel = new wxButton( this, wxID_CANCEL );
        wxButton *btnOk = new wxButton( this, wxID_OK );

        bsButtonsBottom->Add( btnCancel );
        bsButtonsBottom->AddSpacer( 10 );
        bsButtonsBottom->Add( btnOk );
        bsMain->Add( bsButtonsBottom, 0, wxALIGN_RIGHT | wxRIGHT, 10 );
        bsMain->AddSpacer( 20 );



        SetSizer( bsMain );

        Connect( wxID_OK, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( ChooserVariablesDialog::OnOk ) );
        Connect( wxID_ANY, wxEVT_COMMAND_LISTBOX_SELECTED, wxCommandEventHandler( ChooserVariablesDialog::OnListBoxSelected ) );
    }

    ChooserVariablesDialog::~ChooserVariablesDialog()
    {

    }

    EnvironmentVariable ChooserVariablesDialog::GetVariable() const
    {
        const int selection( m_lbEnvironmentVariables->GetSelection() );
        EnvironmentVariable l_variable;

        if( selection != wxNOT_FOUND )
        {
            l_variable.name = m_lbEnvironmentVariables->GetString( selection );
            l_variable.value = m_tcVariableValue->GetValue();
        }

        return l_variable;
    }

    void ChooserVariablesDialog::OnOk( wxCommandEvent &evt )
    {
        const int selection( m_lbEnvironmentVariables->GetSelection() );

        if( selection != wxNOT_FOUND )
            EndModal( wxID_OK );
    }

    void ChooserVariablesDialog::OnListBoxSelected( wxCommandEvent &evt )
    {
        wxString variable;
        wxGetEnv( evt.GetString(), &variable );

        m_tcVariableValue->SetValue( variable );
    }










    NewVariableDialog::NewVariableDialog( wxWindow *parent, wxWindowID id, const wxString &name, const wxString &value )
        : wxDialog( parent, id, wxT( "New environment variable" ), wxDefaultPosition, wxSize( 400, 150 ) )
    {
        wxBoxSizer *bsMain = new wxBoxSizer( wxVERTICAL );

        wxFlexGridSizer *fgsInput = new wxFlexGridSizer( 2, 2, 5, 10 );

        wxStaticText *stVariable = new wxStaticText( this, wxID_ANY, wxT( "Variable: " ) );
        tcVariable = new wxTextCtrl( this, wxID_ANY, wxT( "" ), wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER );
        fgsInput->Add( stVariable );
        fgsInput->Add( tcVariable, 0, wxEXPAND );
        tcVariable->SetFocus();
        tcVariable->SetValue( name );

        wxStaticText *stValue = new wxStaticText( this, wxID_ANY, wxT( "Value: " ) );
        tcValue = new wxTextCtrl( this, wxID_ANY, wxT( "" ), wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER );
        fgsInput->Add( stValue );
        fgsInput->Add( tcValue, 0, wxEXPAND );
        tcValue->SetValue( value );

        fgsInput->AddGrowableCol( 1, 1 );

        bsMain->AddSpacer( 20 );
        bsMain->Add( fgsInput, 1, wxEXPAND | wxRIGHT | wxLEFT, 20 );




        wxBoxSizer *bsButtons = new wxBoxSizer( wxHORIZONTAL );
        wxButton *btnCancel = new wxButton( this, wxID_CANCEL );
        wxButton *btnOk = new wxButton( this, wxID_OK );

        bsButtons->Add( btnCancel );
        bsButtons->AddSpacer( 10 );
        bsButtons->Add( btnOk );

        bsMain->AddSpacer( 10 );
        bsMain->Add( bsButtons, 0, wxALIGN_RIGHT | wxRIGHT, 20 );
        bsMain->AddSpacer( 10 );

        SetSizer( bsMain );



        Connect( wxID_OK, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( NewVariableDialog::OnOk ), NULL, this );
        Connect( wxID_ANY, wxEVT_COMMAND_TEXT_ENTER, wxCommandEventHandler( NewVariableDialog::OnOk ), NULL, this );
    }

    NewVariableDialog::~NewVariableDialog()
    {

    }

    void NewVariableDialog::OnOk( wxCommandEvent &evt )
    {
        if( tcVariable->GetValue().Trim().Trim( false ) != wxEmptyString )
            EndModal( wxID_OK );
    }


    EnvironmentVariable NewVariableDialog::GetVariable() const
    {
        EnvironmentVariable l_variable={ tcVariable->GetValue(), tcValue->GetValue() };
        return l_variable;
    }
}
